package com.example.service.impl;

import com.example.entity.Cases;
import com.example.mapper.CasesMapper;
import com.example.service.CasesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CasesServiceImpl implements CasesService {

    @Autowired
    private CasesMapper casesMapper;

    // 获取案例列表
    @Override
    public Map<String, Object> getCasesByPage(Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Cases> cases = casesMapper.selectCasesByPage(offset, pageSize);
        int total = casesMapper.countCases();
        Map<String, Object> result = new HashMap<>();
        result.put("data", cases);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }

    // 根据标题或作者搜索案例
    @Override
    public Map<String, Object> searchCases(Integer pageNum, Integer pageSize, String titlePart, String author) {
        int offset = (pageNum - 1) * pageSize;
        List<Cases> cases = casesMapper.selectCasesByTitleOrAuthor(offset, pageSize, titlePart, author);
        int total = casesMapper.countCasesByTitleOrAuthor(titlePart, author);
        Map<String, Object> result = new HashMap<>();
        result.put("data", cases);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }

    @Override
    public Cases getCaseById(Integer id) {
        return casesMapper.selectCaseById(id);
    }
}